unit uDynProcImport;

interface
uses
  Windows,SysUtils,Messages, Classes, DB, ADODB, Forms, Dialogs, ExtDlgs,uGlobal;

type  
  TDynProcInit = procedure (AconMain: TADOConnection; AGetSysValue:TGetSysGlobalValue);stdcall;
  TDynProcFree = procedure ();stdcall;

  TDynamicExecProc1 = procedure (AMaster,ADetail:TDataSet; AProcName: string; AOperType: integer;
  AGetValueProc:TGetGlobalValueProc;out ErrMsg:string; out ReturnValue: Boolean);stdcall;
  
  TDynamicExecProc2 = procedure (DataSetArray:array of TDataSet; AProcName: string;AOperType: integer;
  AGetValueProc:TGetGlobalValueProc;Out ErrMsg:string; out ReturnValue: Boolean);stdcall;

  procedure DynamicExecProc(AMaster,ADetail:TDataSet; AProcName: string; AOperType: integer;
    AGetValueProc:TGetGlobalValueProc;out ErrMsg:string; out ReturnValue: Boolean);overload;

  procedure DynamicExecProc(DataSetArray:array of TDataSet; AProcName: string;AOperType: integer;
    AGetValueProc:TGetGlobalValueProc;Out ErrMsg:string; out ReturnValue: Boolean);overload;

  procedure DynamicExecProc(AMaster,ADetail:TDataSet; AProcName: string; AOperType: integer;
    adoConnection: TADOConnection;AGetValueProc:TGetGlobalValueProc;out ErrMsg:string; out ReturnValue: Boolean);overload;

  procedure DynamicExecProc(DataSetArray:array of TDataSet; AProcName: string;AOperType: integer;
    adoConnection: TADOConnection;AGetValueProc:TGetGlobalValueProc;Out ErrMsg:string; out ReturnValue: Boolean);overload;

var
  DynDllHandle:THandle = 0;
  DynProcInit:TDynProcInit = nil;
  DynProcFree:TDynProcFree = nil;
  DynamicExecProc1:TDynamicExecProc1 = nil;
  DynamicExecProc2:TDynamicExecProc2 = nil;
  
procedure LoadDynProcDll;
procedure FreeDynProcDll;

implementation

procedure LoadDynProcDll;
begin
  DynDllHandle := LoadLibrary('DynProc.dll');
  DynProcInit := GetProcAddress(DynDllHandle, 'DynProcInit');
  DynProcFree := GetProcAddress(DynDllHandle, 'DynProcFree');
  DynamicExecProc1:= GetProcAddress(DynDllHandle, 'DynamicExecProc1');
  DynamicExecProc2:= GetProcAddress(DynDllHandle, 'DynamicExecProc2');
end;

procedure FreeDynProcDll;
begin
  DynProcFree();
end;

procedure DynamicExecProc(AMaster,ADetail:TDataSet; AProcName: string; AOperType: integer;
    AGetValueProc:TGetGlobalValueProc;out ErrMsg:string; out ReturnValue: Boolean);overload;
begin
  try
    ReturnValue := False;
    DynamicExecProc1(AMaster,ADetail,AProcName,AOperType,AGetValueProc,ErrMsg,ReturnValue);
  except
    on e:exception do
    begin
      if (e.Message <> 'Operation aborted') then
        raise exception.Create( e.Message)
      else begin
        abort;
      end;
    end;
  end;
end;

procedure DynamicExecProc(DataSetArray:array of TDataSet; AProcName: string;AOperType: integer;
    AGetValueProc:TGetGlobalValueProc;Out ErrMsg:string; out ReturnValue: Boolean);overload;
begin
  try
    ReturnValue := False;
    DynamicExecProc2(DataSetArray, AProcName,AOperType,AGetValueProc,ErrMsg,ReturnValue);
  except
    on e:exception do
    begin
      if (e.Message <> 'Operation aborted') then
        raise exception.Create(e.Message)
      else begin
        abort;
      end;
    end;
  end;
end;

procedure DynamicExecProc(AMaster,ADetail:TDataSet; AProcName: string; AOperType: integer;
    adoConnection: TADOConnection;AGetValueProc:TGetGlobalValueProc;out ErrMsg:string; out ReturnValue: Boolean);overload;
begin
  try
    ReturnValue := False;
    DynamicExecProc1(AMaster,ADetail,AProcName,AOperType,AGetValueProc,ErrMsg,ReturnValue);
  except
    on e:exception do
    begin
      if (e.Message <> 'Operation aborted') then
        raise exception.Create(e.Message)
      else begin
        abort;
      end;
    end;
  end;
end;

procedure DynamicExecProc(DataSetArray:array of TDataSet; AProcName: string;AOperType: integer;
    adoConnection: TADOConnection;AGetValueProc:TGetGlobalValueProc;Out ErrMsg:string; out ReturnValue: Boolean);overload;
begin
  try
    ReturnValue := False;
    DynamicExecProc2(DataSetArray, AProcName,AOperType,AGetValueProc,ErrMsg,ReturnValue);
  except
    on e:exception do
    begin
      if (e.Message <> 'Operation aborted') then
        raise exception.Create(e.Message)
      else begin
        abort;
      end;
    end;
  end;
end;
 
end.
